package com.xujl.elementlib.main;

import android.view.View;
import android.view.ViewStub;

import com.xujl.elementlib.BR;
import com.xujl.elementlib.ElementLog;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

/**
 * @author x
 * @Description 元素抽象基类
 */
public abstract class BaseElement<T extends ViewDataBinding> {
    private static final String TAG = "BaseElement";
    private ViewStub mViewStub;
    private View mRoot;
    protected T mBind;


    public BaseElement (ViewStub viewStub) {
        mViewStub = viewStub;
    }

    public void initView () {
        if (mViewStub != null) {
            mViewStub.setLayoutResource(getLayoutId());
            mBind = DataBindingUtil.bind(mViewStub.inflate());
            if (mBind == null) {
                ElementLog.e(TAG, "mBind init error ！！！");
                return;
            }
            mBind.setVariable(BR.control, this);
            mRoot = mBind.getRoot();
        }
    }


    protected abstract int getLayoutId ();

    public T getBind () {
        return mBind;
    }

    public View getRoot () {
        return mRoot;
    }

    /**
     * 销毁回收资源
     */
    public void destroy () {

    }
}
